package com.csu.petstore.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.csu.petstore.common.CommonResponse;
import com.csu.petstore.pojo.Category;
import com.csu.petstore.pojo.Item;
import com.csu.petstore.pojo.ItemQuantity;
import com.csu.petstore.pojo.Product;
import com.csu.petstore.mapper.ProductMapper;
import com.csu.petstore.mapper.CategoryMapper;
import com.csu.petstore.mapper.ItemMapper;
import com.csu.petstore.mapper.ItemQuantityMapper;
import com.csu.petstore.mapper.ProductMapper;
import com.csu.petstore.service.CatalogService;
import com.csu.petstore.vo.CategoryVo;
import com.csu.petstore.vo.ItemVo;
import com.csu.petstore.vo.ProductVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CatalogServiceImpl implements CatalogService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private ItemQuantityMapper itemQuantityMapper;

    @Override
    public CommonResponse<CategoryVo> getCategory(String categoryId) {
        CategoryVo categoryVo = new CategoryVo();
        Category category = categoryMapper.selectById(categoryId);
        QueryWrapper<Product> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("category", categoryId);
        List<Product> productList =productMapper.selectList(queryWrapper);

        categoryVo.setCategoryId(categoryId);
        categoryVo.setCategoryName(category.getName());
        categoryVo.setProductList(productList);
        return CommonResponse.createForSuccess(categoryVo);
    }

    @Override
    public CommonResponse<ProductVo> getProduct(String productId) {
        ProductVo productVo = new ProductVo();
        Product product = productMapper.selectById(productId);
        QueryWrapper<Item> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("productid", productId);
        List<Item> itemList= itemMapper.selectList(queryWrapper);

        productVo.setProductId(productId);
        productVo.setCategoryId(product.getCategoryId());
        productVo.setProductName(product.getName());
        productVo.setItemList(itemList);
        return CommonResponse.createForSuccess(productVo);
    }
    //获得搜索商品功能业务逻辑的实现
    @Override
    public CommonResponse<CategoryVo> getSearchProduct(String keyword) {
        CategoryVo categoryVo =new CategoryVo();
        QueryWrapper<Product> queryWrapper =new QueryWrapper<>();
        queryWrapper.like("category", keyword);
        List<Product> productList= productMapper.selectList(queryWrapper);
        categoryVo.setProductList(productList);
        return CommonResponse.createForSuccess(categoryVo);
    }


    @Override
    public CommonResponse<ItemVo> getItem(String itemId) {
       ItemVo itemVo = new ItemVo();
       Item item = itemMapper.selectById(itemId);
       Product product = productMapper.selectById(item.getProductId());
       ItemQuantity itemQuantity =itemQuantityMapper.selectById(itemId);

       itemVo.setItemId(itemId);
       itemVo.setListPrice(item.getListPrice());
       itemVo.setAttributes(item.getAttribute1());
       itemVo.setProductId(item.getProductId());
       itemVo.setProductName(product.getName());

       String [] temp = product.getDescription().split("\"");
       itemVo.setDescriptionImage(temp[1]);
       itemVo.setDescriptionText(temp[2].substring(1));
       itemVo.setQuantity(itemQuantity.getQuantity());
       return CommonResponse.createForSuccess(itemVo);
    }
}
